package com.demo2do.fms.admin.service.data.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.data.RoadBridgeFeeDao;
import com.demo2do.fms.admin.entity.search.data.RoadBridgeFeeSearch;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.RoadBridgeFeeService;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.cost.RoadBridgeFee;
import com.demo2do.fms.entity.data.cost.Ton;

/**
 * 
 * @author Ysy
 *
 */
@Service("roadBridgeFeeService")
public class RoadBridgeFeeServiceImpl extends DataServiceImpl<RoadBridgeFee> implements RoadBridgeFeeService {

    @Dao
    private RoadBridgeFeeDao roadBridgeFeeDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RoadBridgeFeeService#selectOrganization(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public Organization selectOrganization(List<Long> orgIds, Page page) {
        
        //if orgIds is null, return null
        if(CollectionUtils.isEmpty(orgIds))
            return null;
        
        int currentPage = page.getCurrentPage();
        
        if(orgIds.size() >= currentPage) {
            
            // calculate page first
            page.with(orgIds.size() * Ton.size() * 2).calculate();
            
            // select organization and return 
            Long orgId = orgIds.get(currentPage - 1);
            
            return genericDaoSupport.load(Organization.class, orgId);
            
        }
        
        return null;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RoadBridgeFeeService#list(com.demo2do.fms.entity.organization.Organization)
     */
    public List<RoadBridgeFee> list(Organization organization) {
        return roadBridgeFeeDao.list(organization.getId());
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RoadBridgeFeeService#list(com.demo2do.fms.admin.entity.search.RoadBridgeFeeSearch)
     */
    @SuppressWarnings("unchecked")
    public List<RoadBridgeFee> list(RoadBridgeFeeSearch roadBridgeFeeSearch) {
        return genericDaoSupport.searchForList(roadBridgeFeeSearch.buildSentenceTemplate(), roadBridgeFeeSearch.buildParameters());
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RoadBridgeFeeService#create(com.demo2do.fms.entity.data.cost.RoadBridgeFee)
     */
    public void create(RoadBridgeFee roadBridgeFee) {
        genericDaoSupport.save(roadBridgeFee.init());
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RoadBridgeFeeService#edit(com.demo2do.fms.entity.data.cost.RoadBridgeFee)
     */
    public void edit(Long operator , RoadBridgeFee roadBridgeFee) {
        genericDaoSupport.update(roadBridgeFee.operator(operator));
    }

}
